﻿using _07_04_DBHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _07_DBHelper的使用
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //绑定下拉框
        private void BindPro()
        {
            DataTable dt = new DataTable();
            DBHelper.PrepareCmd("select * from ProfessionInfo");
            dt = DBHelper.ExecQuery();
            DataRow dr = dt.NewRow();
            dr["professionID"] = "0";
            dr["professionName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
            this.cmdName.DataSource = dt;
            this.cmdName.DisplayMember = "professionName";
            this.cmdName.ValueMember = "professionID";
        }

        //绑定学生数据表
        public void BindData()
        {
            string sql = "select * from StudentInfo inner join ProfessionInfo on StudentInfo.ProfessionID=ProfessionInfo.ProfessionID  where 1 = 1 ";
            if (!this.cmdName.SelectedValue.ToString().Equals("0"))
                sql += " and StudentInfo.ProfessionID = " + this.cmdName.SelectedValue.ToString();
            if (!this.stuname.Text.Equals(""))
                sql += " and StuName like '%" + this.stuname.Text + "%'";
            this.dataGridView1.AutoGenerateColumns = false;
            DBHelper.PrepareCmd(sql);
            this.dataGridView1.DataSource = DBHelper.ExecQuery();

        }

    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            BindPro();
            BindData();

        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }


        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("是否删除该学生?", "提示", MessageBoxButtons.OKCancel);
            if (!(dialogResult.ToString() == "OK"))
            {
                return;
            }
            //获取学生学号
            string selectID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "delete from StudentInfo  where StuID =@StuID" ;
            DBHelper.PrepareCmd(sql);
            DBHelper.SetParamter("StuID",selectID);
            int count = DBHelper.ExecNonQuery();
            if (count > 0)
            {

                MessageBox.Show("删除成功！");
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
            this.BindData();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            FrmAdd frmAdd =new FrmAdd();
            frmAdd.FormClosed += (s, x) =>
            {
                this.BindData();
            };
            frmAdd.Show();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            string selectID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            //MessageBox.Show(selectID);
            FrmEdit frm = new FrmEdit(selectID);
            frm.Owner = this;
            frm.Show();
        }
    }
}
